package com.my.swas.sso.module.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.module.cms.dto.CmsArticleDto;
import com.my.swas.sso.module.cms.entity.CmsArticle;
import com.my.swas.sso.module.cms.entity.CmsBasic;
import com.my.swas.sso.module.cms.mapper.CmsArticleMapper;
import com.my.swas.sso.module.cms.service.CmsArticleService;
import com.my.swas.sso.module.cms.service.CmsBasicService;
import com.my.swas.sso.module.cms.vo.CmsArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @author: :MaYong
 */
@Service
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArticle> implements CmsArticleService{
    @Autowired
    CmsArticleMapper cmsArticleMapper;
    @Resource
    CmsBasicService cmsBasicService;

    @Override
    public IPage articleList(CmsArticleDto cmsArticleDto) {
        IPage<CmsArticleVo> iPage = cmsArticleMapper.selectArticleList(cmsArticleDto.getPage(), cmsArticleDto);
        return iPage;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void insert(CmsArticleDto cmsArticleDto) {
        CmsBasic cmsBasic = new CmsBasic();
        BeanCopierUtil.convert(cmsArticleDto, cmsBasic);
        cmsBasicService.save(cmsBasic);
        CmsArticle cmsArticle = new CmsArticle();
        BeanCopierUtil.convert(cmsArticleDto, cmsArticle);
        cmsArticle.setArticleBasicId(cmsBasic.getBasicId());
        cmsArticle.setArticleAppId(cmsArticleDto.getBasicAppId());
        this.save(cmsArticle);
    }

    @Override
    public void update(CmsArticleDto cmsArticleDto) {
        CmsBasic cmsBasic = new CmsBasic();
        BeanCopierUtil.convert(cmsArticleDto, cmsBasic);
        cmsBasicService.update(new LambdaUpdateWrapper<>(cmsBasic));
        CmsArticle cmsArticle = new CmsArticle();
        BeanCopierUtil.convert(cmsArticleDto, cmsArticle);
        cmsArticle.setArticleBasicId(cmsArticleDto.getBasicId());
        cmsArticle.setArticleAppId(cmsArticleDto.getBasicAppId());
        this.update(new LambdaUpdateWrapper<>(cmsArticle));
    }
}
